//------------------------------------------------------------------------------
//SFWK Framework
//Copyright (C) 2011 SQLI

//This program is free software: you can redistribute it and/or modify
//it under the terms of the GNU General Public License as published by
//the Free Software Foundation, either version 3 of the License, or
//(at your option) any later version.

//This program is distributed in the hope that it will be useful,
//but WITHOUT ANY WARRANTY; without even the implied warranty of
//MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
//GNU General Public License for more details.

//You should have received a copy of the GNU General Public License
//along with this program.  If not, see <http://www.gnu.org/licenses/>.
//------------------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace SFWK.Core.Security
{
    /// <summary>
    /// Defines SFWK Operation and provides permission check methods
    /// </summary>
    public abstract class SFWKOperation
    {
        /// <summary>
        /// List of dependsant operation.  For example, if a user has write operation permission on a module, he must have read operation too.
        /// This list will contain the operation that the user will have obligatory with the current operation
        /// </summary>
        protected List<Type> dependantOperations = new List<Type>();
               

        /// <summary>
        /// Adds dependent operation : For Read, add write for example
        /// </summary>
        /// <typeparam name="T"></typeparam>
        protected void AddDependency<T>() { dependantOperations.Add(typeof(T)); }


        /// <summary>
        /// Checks permission on the current operation, and on the dependant operation too
        /// </summary>
        /// <returns>True if user has permission on the current or dependant operations</returns>
        public virtual bool HasPermission()
        {
            foreach (var op in dependantOperations)
                if (((SFWKOperation)Activator.CreateInstance(op)).HasPermission()) return true;
            return true;
        }

        /// <summary>
        /// Checks contextual permissions
        /// </summary>
        /// <param name="obj"></param>
        /// <returns></returns>
        public virtual bool HasPermissionForContext(object obj)
        {
            return true;
        }
    }
}

